//
//  JQAFNetTool.m
//  QiongYou
//
//  Created by 景强 on 16/3/7.
//  Copyright © 2016年 景强. All rights reserved.
//

#import "JQAFNetTool.h"
#import "AFNetworking.h"

@implementation JQAFNetTool

+ (void)GETNetWithUrl:(NSString *)url body:(id)body headerFile:(NSDictionary *)header response:(JQResponseStyle)responseStyle success:(void (^)(id result))success
              failure:(void (^)(NSError * error))failure{
    // 1 设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
   
    // 2 设置请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    // 3 设置返回数据的类型
    switch (responseStyle) {
        case JQJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
         case JQXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            
            case JQDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    // 4 设置响应数据的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    // 5 UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    
    
    // 6 请求数据
    
    
#pragma mark - 本地缓存
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *otherPath = [NSString stringWithFormat:@"%@/%ld", path, [url hash]];
    
    
    
    [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            
            [NSKeyedArchiver archiveRootObject:responseObject toFile:otherPath];
            
            
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
            
            id result = [NSKeyedUnarchiver unarchiveObjectWithFile:otherPath];
            
            if (result) {
                success(result);
            }
            
            
            failure(error);
        }
        
        
    }];
    
    
}


+ (void)POSTNetWithUrl:(NSString *)url body:(id)body bodyStyle:(JQRequestStyle)bodyRequestStyle headerFile:(NSDictionary *)header response:(JQResponseStyle)responseStyle success:(BlockOfSuccess)success failure:(void (^)(NSError *))failure{
    
    
    // 1 设置网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2 设置body体数据类型
    switch (bodyRequestStyle) {
        case JQRequestJSON:
            
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
            
           case JQRequestString:
           [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
               return parameters;
           }];
            
            
            break;
        default:
            break;
    }
    
    
    // 3 设置请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    // 4 设置返回数据的类型
    switch (responseStyle) {
        case JQJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case JQXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            
        case JQDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    // 5 设置响应数据的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    // 6 UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    
    
    // 7 请求数据
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
            failure(error);
        }
        
        
    }];

    
    
    
    
    
    
    
    
}





@end
